{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 深入探讨tensorflow的工作原理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import tensorflow.compat.v1 as tf  #使用1.0版本的tensorflow\n",
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Shapes: () (4,) (2, 2)\n",
      "Ranks: 0 1 2\n"
     ]
    }
   ],
   "source": [
    "g = tf.Graph()\n",
    "with g.as_default():\n",
    "    t1 = tf.constant(np.pi)\n",
    "    t2 = tf.constant([1, 2, 3, 4])\n",
    "    t3 = tf.constant([[1, 2], [3, 4]])\n",
    "    #查看张量的阶数\n",
    "    r1 = tf.rank(t1)\n",
    "    r2 = tf.rank(t2)\n",
    "    r3 = tf.rank(t3)\n",
    "\n",
    "    #查看维度\n",
    "    s1 = t1.get_shape()\n",
    "    s2 = t2.get_shape()\n",
    "    s3 = t3.get_shape()\n",
    "\n",
    "    print(\"Shapes:\", s1, s2, s3)\n",
    "\n",
    "with tf.Session(graph=g) as sess:\n",
    "    print(\"Ranks:\",\n",
    "          r1.eval(),  #评估节点\n",
    "          r2.eval(),\n",
    "          r3.eval())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](picture/计算图.png)\n",
    "![](picture/计算图2.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "z: 1\n"
     ]
    }
   ],
   "source": [
    "g = tf.Graph()  #初始化一个空白图\n",
    "with g.as_default():\n",
    "    a = tf.constant(1, name=\"a\")  #把节点添加到图上面\n",
    "    b = tf.constant(2, name=\"b\")\n",
    "    c = tf.constant(3, name=\"c\")\n",
    "    z = 2 * (a - b) + c\n",
    "with tf.Session(graph=g) as sess:  #启动一个会话来评估计算图里面的张量节点z\n",
    "    print(\"z:\", sess.run(z))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](picture/占位符.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "z: 1\n"
     ]
    }
   ],
   "source": [
    "g = tf.Graph()\n",
    "with g.as_default():\n",
    "    tf_a = tf.placeholder(tf.int32, shape=[], name='tf_a')  #定义占位符\n",
    "    tf_b = tf.placeholder(tf.int32, shape=[], name='tf_b')\n",
    "    tf_c = tf.placeholder(tf.int32, shape=[], name='tf_c')\n",
    "    r1 = tf_a - tf_b\n",
    "    r2 = 2 * r1\n",
    "    z = r2 + tf_c\n",
    "\n",
    "with tf.Session(graph=g) as sess:\n",
    "    feed_dict = {tf_a: 1, tf_b: 2, tf_c: 3}\n",
    "    print(\"z:\", sess.run(z, feed_dict=feed_dict))  #计算图的节点是占位符节点时，需要提供占位符的数据"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](picture/batchsize.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x mean: [1.3728936 0.6506112]\n",
      "x mean: [0.8470377 0.5684544]\n"
     ]
    }
   ],
   "source": [
    "g = tf.Graph()\n",
    "with g.as_default():\n",
    "    tf_x = tf.placeholder(tf.float32, shape=[None, 2], name=\"tf_x\")  #这样可以传入任意维度的x张量\n",
    "    x_mean = tf.reduce_mean(tf_x, axis=0, name='mean')\n",
    "\n",
    "with tf.Session(graph=g) as sess:\n",
    "    x1 = np.random.uniform(low=0, high=2, size=(5, 2))\n",
    "    print(\"x mean:\", sess.run(x_mean, feed_dict={tf_x: x1}))\n",
    "    x1 = np.random.uniform(low=0, high=2, size=(10, 2))\n",
    "    print(\"x mean:\", sess.run(x_mean, feed_dict={tf_x: x1}))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### tensorflow中的变量\n",
    "![](picture/变量.png)\n",
    "![](picture/变量2.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.Variable 'w:0' shape=(2, 4) dtype=int64>\n"
     ]
    }
   ],
   "source": [
    "g1 = tf.Graph()\n",
    "with g1.as_default():\n",
    "    w = tf.Variable(np.array([[1, 2, 3, 4], [5, 6, 7, 8]]), name=\"w\")\n",
    "    print(w)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4]\n",
      " [5 6 7 8]]\n"
     ]
    }
   ],
   "source": [
    "#理解定义为变量的张量不会被分配内存空间，而且在初始化之前这些变量不包含任何值是很重要的。因此，在计算图中处理任何节点之前，必须初始化存在于要执行节点路径内的变量。这个初始化过程包括为相关张量分配内存空间并为其赋予初始值。TensorFlow提供了一个名为tf.global_variables_initializer的函数，该函数返回初始化所有计算图中现存变量的操作符。执行该操作符初始化变量如下：\n",
    "\n",
    "with tf.Session(graph=g1) as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    print(sess.run(w))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "g2 = tf.Graph()\n",
    "with g2.as_default():\n",
    "    w1 = tf.Variable(np.array([[1, 2, 3, 4], [5, 6, 7, 8]]), name=\"w\")\n",
    "    # init_op=tf.global_variables_initializer()\n",
    "    w2 = tf.Variable(np.array([[1, 2, 3, 4], [5, 6, 7, 8]]), name=\"w\")\n",
    "    init_op = tf.global_variables_initializer()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4]\n",
      " [5 6 7 8]]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session(graph=g2) as sess:\n",
    "    sess.run(init_op)\n",
    "    print(sess.run(w1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4]\n",
      " [5 6 7 8]]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session(graph=g2) as sess:\n",
    "    sess.run(init_op)\n",
    "    print(sess.run(w2))  #init_op要放在所有变量的后面"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](picture/变量3.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.Variable 'net_A/layer_1/weights:0' shape=(10, 4) dtype=float32>\n",
      "<tf.Variable 'net_A/layer_2/weights:0' shape=(20, 10) dtype=float32>\n",
      "<tf.Variable 'net_A/layer_3/weights:0' shape=(10, 4) dtype=float32>\n"
     ]
    }
   ],
   "source": [
    "g = tf.Graph()\n",
    "with g.as_default():\n",
    "    with tf.variable_scope('net_A'):\n",
    "        with tf.variable_scope('layer_1'):  #嵌套的变量域\n",
    "            w1 = tf.Variable(tf.random_normal(shape=(10, 4)), name='weights')\n",
    "        with tf.variable_scope('layer_2'):\n",
    "            w2 = tf.Variable(tf.random_normal(shape=(20, 10)), name='weights')\n",
    "        with tf.variable_scope('layer_3'):\n",
    "            w3 = tf.Variable(tf.random_normal(shape=(10, 4)), name='weights')\n",
    "\n",
    "    print(w1)\n",
    "    print(w2)\n",
    "    print(w3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](picture/变量4.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [],
   "source": [
    "#分类器\n",
    "def bulid_classfier(data, labels, n_classes=2):\n",
    "    data_shape = data.get_shape().as_list()\n",
    "    weights = tf.get_variable(name='weights',\n",
    "                              shape=(data_shape[1], n_classes),\n",
    "                              dtype=tf.float32)\n",
    "    bias = tf.get_variable(name='bias',\n",
    "                           initializer=tf.zeros(shape=n_classes))\n",
    "    logits = tf.add(tf.matmul(data, weights),\n",
    "                    bias,\n",
    "                    name='logits')\n",
    "    return logits, tf.nn.softmax(logits)\n",
    "\n",
    "\n",
    "#生成器，产生隐藏层和输出层\n",
    "def bulid_generator(data, n_hidden):\n",
    "    data_sahpe = data.get_shape().as_list()\n",
    "    w1 = tf.Variable(tf.random_normal(shape=(data_sahpe[1], n_hidden)),\n",
    "                     name='w1')\n",
    "    b1 = tf.Variable(tf.zeros(shape=n_hidden),\n",
    "                     name='b1')\n",
    "    hidden = tf.add(tf.matmul(data, w1), b1,\n",
    "                    name='hidden_pre-activation')\n",
    "    w2 = tf.Variable(tf.random_normal(shape=(n_hidden, data_sahpe[1])),\n",
    "                     name='w2')\n",
    "    b2 = tf.Variable(tf.zeros(shape=data_sahpe[1]),\n",
    "                     name='b2')\n",
    "    output = tf.add(tf.matmul(hidden, w2), b2,\n",
    "                    name='output')\n",
    "    return output, tf.nn.sigmoid(output)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [],
   "source": [
    "batch_size = 64\n",
    "g = tf.Graph()\n",
    "with g.as_default():\n",
    "    tf_X = tf.placeholder(shape=(batch_size, 100),\n",
    "                          dtype=tf.float32,\n",
    "                          name='tf_X')\n",
    "    with tf.variable_scope('generator'):  #生成器里面的变量\n",
    "        gen_out1 = bulid_generator(data=tf_X, n_hidden=50)  #产生输出层数据\n",
    "\n",
    "    with tf.variable_scope('classfier') as scope:\n",
    "        cls_out1 = bulid_classfier(data=tf_X, labels=tf.ones(shape=batch_size))  #直接放x的分类结果\n",
    "        #复用变量\n",
    "        scope.reuse_variables()  #复用一下上面产生的输出层数据，将其传输到分类器里面，复用变量才能使用gen_out1\n",
    "        cls_out2 = bulid_classfier(data=gen_out1[1],\n",
    "                                   labels=tf.ones(shape=batch_size))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [],
   "source": [
    "g = tf.Graph()\n",
    "with g.as_default():\n",
    "    tf_X = tf.placeholder(shape=(batch_size, 100),\n",
    "                          dtype=tf.float32,\n",
    "                          name='tf_X')\n",
    "    with tf.variable_scope('generator'):\n",
    "        gen_out1 = bulid_generator(data=tf_X, n_hidden=50)\n",
    "\n",
    "    with tf.variable_scope('classfier') as scope:\n",
    "        cls_out1 = bulid_classfier(data=tf_X, labels=tf.ones(shape=batch_size))\n",
    "        #复用变量\n",
    "        #scope.reuse_variables()\n",
    "    with tf.variable_scope('classfier', reuse=True):\n",
    "        cls_out2 = bulid_classfier(data=gen_out1[1],\n",
    "                                   labels=tf.ones(shape=batch_size))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](picture/做回归模型.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [],
   "source": [
    "g=tf.Graph()\n",
    "with g.as_default():\n",
    "    tf.set_random_seed(123)\n",
    "\n",
    "    tf_X=tf.placeholder(shape=(None),\n",
    "                        dtype=tf.float32,\n",
    "                        name=\"tf_X\")\n",
    "    tf_y=tf.placeholder(shape=(None),\n",
    "                        dtype=tf.float32,\n",
    "                        name='tf_y')\n",
    "\n",
    "    weight=tf.Variable(tf.random_normal(shape=(1, 1),stddev=0.25),\n",
    "                  name='weight')\n",
    "\n",
    "    bias=tf.Variable(0.0,name='bias')\n",
    "\n",
    "    #建模\n",
    "    y_hat=tf.add(weight*tf_X,bias,name='y_hat')\n",
    "\n",
    "    cost=tf.reduce_mean(tf.square(y_hat-tf_y),\n",
    "                        name='cost')\n",
    "\n",
    "    #训练模型\n",
    "    opt=tf.train.GradientDescentOptimizer(learning_rate=0.001)\n",
    "    train_op=opt.minimize(cost,name='train_op')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "np.random.seed(0)\n",
    "\n",
    "def make_random_data(size):\n",
    "    x=np.random.uniform(low=-2,high=4,size=size)\n",
    "    y=[]\n",
    "    for t in x:\n",
    "        r=np.random.normal(loc=0,scale=(0.5+t*t/3),size=None)\n",
    "        y.append(r)\n",
    "    return x,1.726*x-0.84+np.array(y)\n",
    "\n",
    "x,y=make_random_data(200)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD4CAYAAADvsV2wAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAmE0lEQVR4nO3df5BcZb3n8fc3QyMTZZlQRIWBAOVSYcUYIlOAm6q7gj/CLyEXdIWrXne1KsteqRLKzUpKS9GiiuxN3d9aYrxa91pSGN3AyJVcAxbZ4voDYUISQyTxIiJkQkkUBsQMZib57h/TPZzpOef0OX1O9zl9+vOqSmWm+0yfpyfp73nO9/k+z2PujoiIVN+CohsgIiLdoYAvItInFPBFRPqEAr6ISJ9QwBcR6RPHFN2AOCeddJKfccYZRTdDRKRnbN++/bfuvjjsuVIH/DPOOIOxsbGimyEi0jPM7NdRzymlIyLSJxTwRUT6hAK+iEifUMAXEekTCvgiIn2i1FU6IiKdNLpjnA1b93FgYpJThgZZu2opq1cMF92sjlHAF5G+NLpjnHV37WZy6ggA4xOTrLtrN0Blg75SOiLSlzZs3Tcb7Bsmp46wYeu+glrUeQr4ItKXDkxMpnq8ChIHfDP7upk9Z2aPBR7bYGZ7zexnZna3mQ1F/OxTZrbbzHaamabOikjhThkaTPV4FaTp4f8TcEnTY/cDb3H3twK/ANbF/PxF7n6uu4+ka6KISP7WrlrKYG1gzmODtQHWrlpaUIs6L3HAd/cHgeebHrvP3afr3z4EnJpj20REOmb1imFuu3oZw0ODGDA8NMhtVy+r7IAt5Ful81FgU8RzDtxnZg58xd03Rr2Ima0B1gAsWbIkx+aJiMy1esVwpQN8s1wGbc3s08A0c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j25MHdJ8zs/zEz1tJzAR9YCVxpZpcBxwH/wcy+6e4fyuPFK5nSMbNLgE8BV7r7oaLb0+ceAc4yszPN7FjgWuCegtvU98zMgK8Bj7v7XxfdnizMbHGjEs/MBoF3AXsLbVSb3H2du5/q7mcw81l5IK9gDxUN+MAXgeOB+81sp5ndXnSD2mVmf2pm+4G3A/ea2dai25RGffD8BmArMwOD33b3PcW2qj1mdifwE2Cpme03s48V3aYMVgIfBi6uf0Z21nuVvehkYJuZ/YyZDsb97p5rOWNVaGkFEZE+UdUevoiINFHAFxHpEwr4IiJ9QgFfRKRPKOCLiPQJBXwRkT6hgC8i0if+PwsuO8xtOoweAAAAAElFTkSuQmCC\n"
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(x,y,'o')\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch:   0:10.0121\n",
      "Epoch:  50:7.3338\n",
      "Epoch: 100:6.0509\n",
      "Epoch: 150:5.4111\n",
      "Epoch: 200:5.0714\n",
      "Epoch: 250:4.8749\n",
      "Epoch: 300:4.7494\n",
      "Epoch: 350:4.6613\n",
      "Epoch: 400:4.5946\n",
      "Epoch: 450:4.5414\n"
     ]
    }
   ],
   "source": [
    "x_train,x_test=x[:100],x[100:]\n",
    "y_train,y_test=y[:100],y[100:]\n",
    "\n",
    "n_epoch=500\n",
    "training_cost=[]\n",
    "with tf.Session(graph=g) as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "\n",
    "    for e in range(n_epoch):\n",
    "        c,_=sess.run([cost,train_op],feed_dict={tf_X:x_train,tf_y:y_train})\n",
    "        training_cost.append(c)\n",
    "        if not e%50 :\n",
    "            print('Epoch:%4d:%.4f'%(e,c))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAd2klEQVR4nO3deXQcZ5nv8e/T3erWbu2SbVmSF9nxnsSKQxJnISvZCIGQZZgQMnDDdiHAH3fCMJdZGC4XThgInIFJIJCwJXNuYg8QINhZjYntWHYcL/G+y5sky7J2tZb3/tFtR3Fsx5ZaKlX373NOn65+u6R63j7Jz6WnqqvMOYeIiPhPwOsCRERkcBTgIiI+pQAXEfEpBbiIiE8pwEVEfCo0khsrKipyVVVVI7lJERHfW716daNzrvjk8REN8KqqKmpra0dykyIivmdme041rhaKiIhPKcBFRHxKAS4i4lMKcBERn1KAi4j4lAJcRMSn3jPAzexnZlZvZhsGjBWY2RIz2xZ/zh/eMkVE5GRnswf+BPCBk8YeAl50zlUDL8ZfD5uXt9Tzo1e2D+cmRER85z0D3Dm3FGg6afg24Mn48pPAhxJb1jst33GE7y/ZRldP33BuRkTEVwbbAy91zh0EiD+XnG5FM3vAzGrNrLahoWFQG5tXmU+0r58N+48NrloRkSQ07AcxnXOPOedqnHM1xcXv+ir/WZlXGWux1+45msjSRER8bbABftjMxgLEn+sTV9K7FWVHmFiURe1uBbiIyHGDDfDfAffFl+8DfpuYck6vpjKf1Xua0D08RURizuY0wqeA5cA0M6szs08C/xe4zsy2AdfFXw+rmqp8jnb0sKOhfbg3JSLiC+95OVnn3D2neeuaBNdyRvMqCwBYvaeJKSXZI7lpEZFRyTffxJxcnEV+Zpr64CIicb4JcDNjXmWBzkQREYnzTYBDrA++q7GdxrZur0sREfGcvwI8fj74au2Fi4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ZxPY1jn7IcAHw/sG/C6Lj6WrEqdcwchFnhASXw8qT4HM6sCLiC2R5rUc463EtYC9cAS51zSzxn4PvC/gIG3cE/2OTtgsZmtNrMH4mPDOmc/3NTYTjGWiuc+Js3nYGbZwLPAl5xzLWanmlps1VOM+W7Ozrk+4HwzywMWmdmsM6zu+zmb2S1AvXNutZlddTY/cooxX8057jLn3AEzKwGWmNnmM6ybkDn7YQ+8Dpgw4HU5cMCjWkbCYTMbCxB/ro+PJ8XnYGZpxML71865hfHhpJ7zcc65ZuAV4AMk95wvAz5oZruJtTyvNrNfkdxzxjl3IP5cDywi1hIZ1jn7IcBXAdVmNtHMwsDdwO88rmk4/Q64L758H/DbAeN3m1nEzCYC1cDrHtQ3aBbb1X4c2OSc+/cBbyXznIvje96YWQZwLbCZJJ6zc+6rzrly51wVsf9fX3LO/S1JPGczyzKznOPLwPXABoZ7zl4fuT3Lo7s3ETtjYQfwNa/rSeC8ngIOAj3E/kX+JFAIvAhsiz8XDFj/a/HPYAtwo9f1D2K+C4j9mbgOWBt/3JTkc54DvBGf8wbg6/HxpJ3zSfO/irfPQknaORM7S+7N+GPj8Zwa7jnrq/QiIj7lhxaKiIicggJcRMSnFOAiIj6lABcR8SkFuIiITynARUR8SgEuIuJT/x/Vzqpcnoe5egAAAABJRU5ErkJggg==\n"
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(training_cost)\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch:   0:10.0121\n",
      "Epoch:  50:7.3338\n",
      "Epoch: 100:6.0509\n",
      "Epoch: 150:5.4111\n",
      "Epoch: 200:5.0714\n",
      "Epoch: 250:4.8749\n",
      "Epoch: 300:4.7494\n",
      "Epoch: 350:4.6613\n",
      "Epoch: 400:4.5946\n",
      "Epoch: 450:4.5414\n"
     ]
    }
   ],
   "source": [
    "#在许多情况下，通过名称来执行变量和操作符非常有用。例如，我们可能在某个独立的模块开发了一个模型，根据Python的作用域规则，从其他不同的Python域无法访问该模型。然而，如果有计算图，就可以用计算图上的张量名来执行图中的节点。\n",
    "n_epoch=500\n",
    "training_cost=[]\n",
    "with tf.Session(graph=g) as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "\n",
    "    for e in range(n_epoch):\n",
    "        c,_=sess.run(['cost:0','train_op'],feed_dict={'tf_X:0':x_train,'tf_y:0':y_train})\n",
    "        training_cost.append(c)\n",
    "        if not e%50 :\n",
    "            print('Epoch:%4d:%.4f'%(e,c))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](picture/保存模型.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch:   0:10.0121\n",
      "Epoch:  50:7.3338\n",
      "Epoch: 100:6.0509\n",
      "Epoch: 150:5.4111\n",
      "Epoch: 200:5.0714\n",
      "Epoch: 250:4.8749\n",
      "Epoch: 300:4.7494\n",
      "Epoch: 350:4.6613\n",
      "Epoch: 400:4.5946\n",
      "Epoch: 450:4.5414\n"
     ]
    }
   ],
   "source": [
    "n_epoch=500\n",
    "training_cost=[]\n",
    "with g.as_default():\n",
    "    saver=tf.train.Saver()\n",
    "with tf.Session(graph=g) as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "\n",
    "    for e in range(n_epoch):\n",
    "        c,_=sess.run(['cost:0','train_op'],feed_dict={'tf_X:0':x_train,'tf_y:0':y_train})\n",
    "        training_cost.append(c)\n",
    "        if not e%50 :\n",
    "            print('Epoch:%4d:%.4f'%(e,c))\n",
    "        saver.save(sess,'data/trained_model')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from data/trained_model\n"
     ]
    },
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD4CAYAAADvsV2wAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAzL0lEQVR4nO2deZgU9bX3P2dYHVCWYVRgYEBFjfsyIxqcHhM1QcIbl0efaNBgYiSJ0cSY3BsN9743yY0323uzadSLmoQoiYlJuBglGrJgDyo6gIrgBhonDiiyOgzIOuf949dNV/dU79XLdJ/P88zT3VXVVb/q6T516vy+5xxRVQzDMIzKp6bUAzAMwzCKgxl8wzCMKsEMvmEYRpVgBt8wDKNKMINvGIZRJfQv9QBSMWrUKJ0wYUKph2EYhtFnWL58+SZVrfdbV9YGf8KECSxbtqzUwzAMw+gziEhHsnUW0jEMw6gSzOAbhmFUCWbwDcMwqgQz+IZhGFWCGXzDMIwqoaxVOoZhGIWksxPa22HjRqivh+ZmaGgo9agKh3n4hmFUJZ2dsGAB7NwJhx3mHhcscMsrFTP4hmFUJe3tMHw4HHII1NS4x+HD3fJKxQy+YRhVycaNMHRo/LKhQ93ySiVjgy8iPxORd0RklWfZ90XkZRFZKSLzRWR4kve+ISIviMhzImKps4ZhlJz6eujujl/W3e2WVyrZePi/AKYmLFsEnKCqJwGvArekeP8HVPUUVW3KboiGYRjB09wM27ZBVxf09LjHbdvc8kolY4OvqmFgS8KyP6vqvsjLpUAFz28bhlFJNDTAhRdCbS1s2OAeL7ywslU6QcoyPwX8Jsk6Bf4sIgr8j6rOSbYT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hGRCcCpwNMlHkrOREIgzwHvAItUtc+eC/Aj4F+BnqB33Oc7XonIX4DDfVbNVtUFkW1m425h5xVzbNmQyXn0YcRnWZ/1wCoJERkK/B64UVW7Sj2eXFHV/cApkXm6+SJygqr2uXkWEZkOvKOqy0XknKD33+cNvqqel2q9iMwEpgPnahknHaQ7jz5OJzDO87oBWF+isRgRRGQAztjPU9U/lHo8QaCq20RkMW6epc8ZfGAK8FERmQYMBg4RkftV9cogdl7RIR0RmQp8Ffioqu4s9XiqmHZgkohMFJGBwOXAQyUeU1UjIgLcC7ykqj8o9XjyQUTqowo8ETkIOA94uaSDyhFVvUVVG1R1Au538regjD1UuMEHbgcOBhaJyHMiclepB5QLInKxiHQCZwGPiMhjpR5TNkQmzq8HHsNNDv5WVVeXdlS5ISK/Bp4CjhGRThG5ptRjypEpwFXAByO/jeciXmVfZDTwdxFZiXMuFqlqoHLGSsFKKxiGYVQJle7hG4ZhGBHM4BuGYVQJZvANwzCqBDP4hmEYVYIZfMMwjCrBDL5hGEaVYAbfMAyjSvj/FOh5cnKqwRwAAAAASUVORK5CYII=\n"
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "g2 = tf.Graph()\n",
    "x_arr = np.arange(-2, 4, 0.1)\n",
    "with tf.Session(graph=g2) as sess:\n",
    "    new_saver = tf.train.import_meta_graph(\n",
    "        'data/trained_model.meta')\n",
    "    new_saver.restore(sess, 'data/trained_model')\n",
    "\n",
    "    y_arr = sess.run('y_hat:0',\n",
    "                      feed_dict={'tf_X:0' : x_arr})\n",
    "\n",
    "plt.figure()\n",
    "plt.plot(x_train, y_train, 'bo')\n",
    "plt.plot(x_test, y_test, 'bo', alpha=0.3)\n",
    "plt.plot(x_arr, y_arr.T[:, 0], '-r', lw=3)\n",
    "# plt.savefig('images/14_05.png', dpi=400)\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](picture/多维张量.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"T1:0\", shape=(3, 4), dtype=float64)\n",
      "The shape of T1 is : (3, 4)\n",
      "<tf.Variable 'Variable:0' shape=(3, 4) dtype=float32>\n",
      "<tf.Variable 'Variable_1:0' shape=(3,) dtype=float32>\n"
     ]
    }
   ],
   "source": [
    "g=tf.Graph()\n",
    "with g.as_default():\n",
    "    arr=np.array([[1.,2.,3.,4.],\n",
    "                  [5.,6.,7.,8.],\n",
    "                  [9.,10.,11.,12.]])\n",
    "    T1=tf.constant(arr,name=\"T1\")\n",
    "    print(T1)\n",
    "    s=T1.get_shape()\n",
    "    print(\"The shape of T1 is :\",s)\n",
    "    T2=tf.Variable(tf.random_normal(shape=s))\n",
    "    print(T2)\n",
    "    T3=tf.Variable(tf.random_normal(\n",
    "        shape=(s.as_list()[0],)\n",
    "    ))\n",
    "    print(T3)\n",
    "#注意到我们用s创建T2，但无法用切片或者索引创建T3。因此通过调用s.as_list（）把s转换成常规的Python列表，然后用一般的索引方法访问。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"T4:0\", shape=(1, 1, 12), dtype=float64)\n",
      "Tensor(\"T5:0\", shape=(1, 3, 4), dtype=float64)\n"
     ]
    }
   ],
   "source": [
    "with g.as_default():\n",
    "    T4=tf.reshape(T1,shape=[1,1,-1],#-1会自动分配维度\n",
    "                  name=\"T4\")\n",
    "    print(T4)\n",
    "    T5=tf.reshape(T1,shape=[1,3,-1],\n",
    "                  name='T5')\n",
    "    print(T5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12.]]] \n",
      "\n",
      "[[[ 1.  2.  3.  4.]\n",
      "  [ 5.  6.  7.  8.]\n",
      "  [ 9. 10. 11. 12.]]]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session(graph=g) as sess:\n",
    "    print(sess.run(T4),'\\n')\n",
    "    print(sess.run(T5))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"T6:0\", shape=(4, 3, 1), dtype=float64)\n",
      "Tensor(\"T7:0\", shape=(1, 4, 3), dtype=float64)\n",
      "[<tf.Tensor 'T8_1:0' shape=(1, 3, 2) dtype=float64>, <tf.Tensor 'T8_1:1' shape=(1, 3, 2) dtype=float64>]\n"
     ]
    }
   ],
   "source": [
    "with g.as_default():\n",
    "    T6 = tf.transpose(T5, perm=[2, 1, 0],#自定义翻转顺序\n",
    "                     name='T6')\n",
    "    print(T6)\n",
    "    T7 = tf.transpose(T5, perm=[0, 2, 1],\n",
    "                     name='T7')\n",
    "    print(T7)\n",
    "    t5_split=tf.split(T5,\n",
    "                      num_or_size_splits=2,\n",
    "                      axis=2,name=\"T8\")\n",
    "    print(t5_split)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[[ 1.,  2.],\n",
      "        [ 5.,  6.],\n",
      "        [ 9., 10.]]]), array([[[ 3.,  4.],\n",
      "        [ 7.,  8.],\n",
      "        [11., 12.]]])]\n",
      "[[[ 1.]\n",
      "  [ 5.]\n",
      "  [ 9.]]\n",
      "\n",
      " [[ 2.]\n",
      "  [ 6.]\n",
      "  [10.]]\n",
      "\n",
      " [[ 3.]\n",
      "  [ 7.]\n",
      "  [11.]]\n",
      "\n",
      " [[ 4.]\n",
      "  [ 8.]\n",
      "  [12.]]]\n",
      "[[[ 1.  5.  9.]\n",
      "  [ 2.  6. 10.]\n",
      "  [ 3.  7. 11.]\n",
      "  [ 4.  8. 12.]]]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session(graph=g) as sess:\n",
    "    print(sess.run(t5_split))\n",
    "    print(sess.run(T6))\n",
    "    print(sess.run(T7))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"t1:0\", shape=(5, 1), dtype=float32)\n",
      "Tensor(\"t2:0\", shape=(5, 1), dtype=float32)\n",
      "Tensor(\"t3:0\", shape=(10, 1), dtype=float32)\n",
      "Tensor(\"t4:0\", shape=(5, 2), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "#张量的拼接,和数组的拼接是一样的\n",
    "g=tf.Graph()\n",
    "with g.as_default():\n",
    "    t1=tf.ones(shape=(5,1),\n",
    "               dtype=tf.float32,\n",
    "               name='t1')\n",
    "    t2=tf.zeros(shape=(5,1),\n",
    "                dtype=tf.float32,\n",
    "                name='t2')\n",
    "    print(t1)\n",
    "    print(t2)\n",
    "\n",
    "with g.as_default():\n",
    "    t3=tf.concat([t1,t2],axis=0,name='t3')\n",
    "    t4=tf.concat([t1,t2],axis=1,name='t4')\n",
    "\n",
    "    print(t3)\n",
    "    print(t4)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      " [[0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]] \n",
      " [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]] \n",
      " [[1. 0.]\n",
      " [1. 0.]\n",
      " [1. 0.]\n",
      " [1. 0.]\n",
      " [1. 0.]]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session(graph=g) as sess:\n",
    "    print(sess.run(t1),'\\n',sess.run(t2),'\\n',sess.run(t3),'\\n',sess.run(t4))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](picture/控制流.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Objiect: Tensor(\"res:0\", dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "g=tf.Graph()\n",
    "x,y=1,2\n",
    "with g.as_default():\n",
    "    tf_x=tf.placeholder(shape=None,\n",
    "                        dtype=tf.float32,\n",
    "                        name='tf_x')\n",
    "    tf_y=tf.placeholder(shape=None,\n",
    "                        dtype=tf.float32,\n",
    "                        name='tf_y')\n",
    "    if x<y:\n",
    "        res=tf.add(tf_x,tf_y,name='res')\n",
    "    else:\n",
    "        res=tf.subtract(tf_x,tf_y,name='res')\n",
    "    print('Objiect:',res)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n",
      "3.0\n"
     ]
    }
   ],
   "source": [
    "with tf.Session(graph=g) as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    print(sess.run(res,feed_dict={tf_x:x,tf_y:y}))\n",
    "    x,y=2,1\n",
    "    print(sess.run(res,feed_dict={tf_x:x,tf_y:y}))\n",
    "#TensorFlow的计算图是静态的，这意味着一旦建立了计算图，它在执行过程中保持不变。因此，即使改变x和y的值并将新值输入到图中，这些新张量还将会在图中经过相同的路径。因此，在x＝2，y＝1和x＝1，y＝2两种情况下，得到了相同的输出值3.0。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Objiect: Tensor(\"cond_1/Identity:0\", dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "#得用tensorflow的控制流\n",
    "with g.as_default():\n",
    "    tf_x=tf.placeholder(shape=None,\n",
    "                        dtype=tf.float32,\n",
    "                        name='tf_x')\n",
    "    tf_y=tf.placeholder(shape=None,\n",
    "                        dtype=tf.float32,\n",
    "                        name='tf_y')\n",
    "    res=tf.cond(tf_x<tf_y,lambda :tf.add(tf_x,tf_y,name='res_add'),\n",
    "                lambda :tf.subtract(tf_x,tf_y,name='res_sub'))\n",
    "    print('Objiect:',res)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "with tf.Session(graph=g) as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    x,y=1,2\n",
    "    print(sess.run(res,feed_dict={tf_x:x,tf_y:y}))\n",
    "    x,y=2,1\n",
    "    print(sess.run(res,feed_dict={tf_x:x,tf_y:y}))\n",
    "#tf.case tf."
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](picture/tensorbord.png)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [],
   "source": [
    "def build_classifier(data, labels, n_classes=2):\n",
    "    data_shape = data.get_shape().as_list()\n",
    "    weights = tf.get_variable(name = 'weights',\n",
    "                              shape=(data_shape[1],\n",
    "                                     n_classes),\n",
    "                              dtype=tf.float32)\n",
    "    bias = tf.get_variable(name='bias',\n",
    "                           initializer=tf.zeros(\n",
    "                                     shape=n_classes))\n",
    "    print(weights)\n",
    "    print(bias)\n",
    "    logits = tf.add(tf.matmul(data, weights),\n",
    "                    bias,\n",
    "                    name='logits')\n",
    "    print(logits)\n",
    "    return logits, tf.nn.softmax(logits)\n",
    "\n",
    "\n",
    "def build_generator(data, n_hidden):\n",
    "    data_shape = data.get_shape().as_list()\n",
    "    w1 = tf.Variable(\n",
    "        tf.random_normal(shape=(data_shape[1],\n",
    "                                n_hidden)),\n",
    "        name='w1')\n",
    "    b1 = tf.Variable(tf.zeros(shape=n_hidden),\n",
    "                     name='b1')\n",
    "    hidden = tf.add(tf.matmul(data, w1), b1,\n",
    "                    name='hidden_pre-activation')\n",
    "    hidden = tf.nn.relu(hidden, 'hidden_activation')\n",
    "\n",
    "    w2 = tf.Variable(\n",
    "        tf.random_normal(shape=(n_hidden,\n",
    "                                data_shape[1])),\n",
    "        name='w2')\n",
    "    b2 = tf.Variable(tf.zeros(shape=data_shape[1]),\n",
    "                     name='b2')\n",
    "    output = tf.add(tf.matmul(hidden, w2), b2,\n",
    "                    name = 'output')\n",
    "    return output, tf.nn.sigmoid(output)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.Variable 'classifier/weights:0' shape=(100, 2) dtype=float32>\n",
      "<tf.Variable 'classifier/bias:0' shape=(2,) dtype=float32>\n",
      "Tensor(\"classifier/logits:0\", shape=(64, 2), dtype=float32)\n",
      "<tf.Variable 'classifier/weights:0' shape=(100, 2) dtype=float32>\n",
      "<tf.Variable 'classifier/bias:0' shape=(2,) dtype=float32>\n",
      "Tensor(\"classifier/logits_1:0\", shape=(64, 2), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "batch_size=64\n",
    "g = tf.Graph()\n",
    "\n",
    "with g.as_default():\n",
    "    tf_X = tf.placeholder(shape=(batch_size, 100),\n",
    "                          dtype=tf.float32,\n",
    "                          name='tf_X')\n",
    "\n",
    "    ## build the generator\n",
    "    with tf.variable_scope('generator'):\n",
    "        gen_out1 = build_generator(data=tf_X,\n",
    "                                   n_hidden=50)\n",
    "\n",
    "    ## build the classifier\n",
    "    with tf.variable_scope('classifier') as scope:\n",
    "        ## classifier for the original data:\n",
    "        cls_out1 = build_classifier(data=tf_X,\n",
    "                                    labels=tf.ones(\n",
    "                                        shape=batch_size))\n",
    "\n",
    "        ## reuse the classifier for generated data\n",
    "        scope.reuse_variables()\n",
    "        cls_out2 = build_classifier(data=gen_out1[1],\n",
    "                                    labels=tf.zeros(\n",
    "                                        shape=batch_size))\n",
    "\n",
    "\n",
    "with tf.Session(graph = g) as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "\n",
    "    file_writer = tf.summary.FileWriter(logdir='data/logs/', graph=g)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [],
   "source": [
    "# 在终端运行 tensorboard --logdir data/logs/"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 本章详细介绍了TensorFlow的主要功能和概念。首先讨论了TensorFlow的主要功能和优势，以及阶和张量等TensorFlow概念。然后研究了量。接着介绍了评估张量和执行运算符的不同方法，即使用Python变量或引用图中的张量名。我们进一步探讨了一些基本的TensorFlow运算符和张量变换函数，例如tf.transpose、tf.reshape、tf.split和tf.concat。最后介绍了如何使用TensorBoard可视化Tensor-Flow计算图。用该模块可视化计算图非常有用，特别是在调试复杂模型时。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}